package gv.trans;

import gv.trans.util.Utils;

import java.util.ArrayList;

import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.shape.IShape;
import org.anddev.andengine.entity.shape.RectangularShape;
import org.anddev.andengine.entity.sprite.BaseSprite;
import org.anddev.andengine.input.touch.TouchEvent;

public class DropDown extends BaseSprite {

	ArrayList<IShape> items;
	float x,y,w,h;
	float point = Utils.toScreenY(2);

	public DropDown(float x, float y, float w, float h) {
		super(x, y, w, h, Utils.getTiledRegion("transparency.png", 1));
		this.h = h;
		this.x = x;
		this.y = y;
		this.w = w;
		//Draw the borders
		Rectangle r1 = new Rectangle(0,0,Utils.toScreenX(1),h);
		Rectangle r2 = new Rectangle(0,h,w,Utils.toScreenY(1));
		Rectangle r3 = new Rectangle(w-Utils.toScreenX(1),0,Utils.toScreenX(1),h);
		Rectangle r4 = new Rectangle(0,0,w,Utils.toScreenY(1));
		super.attachChild(r1);super.attachChild(r2);super.attachChild(r3);super.attachChild(r4);
		
		items = new ArrayList<IShape>();
	}

	public void attachChild(IShape pEntity) {
		items.add(pEntity); 
		super.attachChild(pEntity);
		//Fix horizontal size
		if(pEntity.getWidth()>w-Utils.toScreenX(2)){
			RectangularShape bs = (RectangularShape)pEntity;
			bs.setWidth(w-Utils.toScreenX(2));
		}
		calculate();
	}

	private void calculate() {
		float leftSpace = h-Utils.toScreenY(1);
		int currentFirstItem = checkCurrentFirst();
		//Delete the others
		for(int i = 0; i<currentFirstItem; i++){
			fill(0,items.get(i));
		}
		float firstH = (items.get(currentFirstItem).getBaseHeight())-(point-base(currentFirstItem));
		firstH = Math.min(firstH, leftSpace);
		fill(firstH,items.get(currentFirstItem));
		items.get(currentFirstItem).setPosition(Utils.toScreenX(1), Utils.toScreenY(1));
		leftSpace = leftSpace-firstH;
		for(int i = currentFirstItem+1; i < items.size(); i++){
			float h_it = items.get(i).getBaseHeight();
			if(!(leftSpace>=h_it)){
				fill(leftSpace, items.get(i));
			}
			else{
				//Prevent drawing problems in case of fast scrolling
				fill(items.get(i).getBaseHeight(), items.get(i));
			}
			items.get(i).setPosition(Utils.toScreenX(1), h-leftSpace);
			leftSpace = leftSpace - items.get(i).getHeight();
		}
	}

	private int base(int currentFirstItem) {
		int tot = 0;
		for(int i = 0; i<currentFirstItem; i++){
			tot+=items.get(i).getBaseHeight();
		}
		return tot;
	}

	private int checkCurrentFirst() {
		int i = 0;
		for(; i < items.size(); i++){
			if(point>=base(i)){
				if(point<base(i)+items.get(i).getBaseHeight()){
					return i;
				}
			}
		}
		return i;
	}

	private void fill(float leftSpace, IShape iShape) {
		RectangularShape bs = (RectangularShape)iShape;
		bs.setHeight(leftSpace);
	}

	float offset = 0;
	float coo = 0;
	boolean dragging = false;
	@Override
	public boolean onAreaTouched(final TouchEvent event,
			final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
		if(event.getAction()==0){
			if((event.getX()>=x&&event.getX()<=x+w)&&(event.getY()>=y&&event.getY()<=y+h)){
				coo = event.getY(); offset = 0;
				dragging = true;
			}
		}
		if(event.getAction()==2&&dragging){
			offset = event.getY()-coo;
			point = point+(offset);
			if(point<Utils.toScreenY(1))point=Utils.toScreenY(1);
			if(point>maxPoint())point=maxPoint();
			coo = event.getY();
		}
		if(event.getAction()==1){
			dragging = false;
		}
		calculate();
		return true;
	}

	private float maxPoint() {
		//Sum of all minus h
		return base(items.size()-1)+items.get(items.size()-1).getBaseHeight()-h;
	}

}
